package com.itheima.controller;

import com.itheima.common.JsonResult;
import com.itheima.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author wuzhiyong
 */
@RestController
@RequestMapping(value = "/common")
public class UploadController {
    @Autowired
    private UploadService uploadService;

    /**
     * 上传图片
     * @param file file
     * @return JsonResult
     * @throws IOException IOException
     */
    @PostMapping (value = "/upload")
    public JsonResult<String> commonUpload(MultipartFile file) throws IOException {
        String upload = uploadService.upload(file);
        if (upload!=null){
            return JsonResult.success(upload, "上传成功");
        }
        return JsonResult.error("上传失败");
    }
}
